// This example shows how to copy parameters from a weather board
// locally


#if defined(WEATHER_I2C_ADDRESS) || defined(SCALE_I2C_ADDRESS)

#ifdef WEATHER_I2C_ADDRESS
#define WEATHER_TEMPERATURE_EXT     2
#define WEATHER_TEMPERATURE_IN      3
#define WEATHER_TEMPERATURE_BOARD   4
#define WEATHER_HUMIDITY            5
#define WEATHER_HUMIDITY_TEMP       6
#define WEATHER_LUMINOSITY          7
#define WEATHER_PRESSURE            8
#endif

#ifdef SCALE_I2C_ADDRESS
#define SCALE_PARAM_WEIGHT                2
#define SCALE_PARAM_WEIGHT_G              3
#define SCALE_PARAM_WEIGHT_OFFSET         4
#define SCALE_PARAM_WEIGHT_FACTOR         5
#define SCALE_PARAM_WEIGHT_MIN            6
#define SCALE_PARAM_WEIGHT_MAX            7
#endif

#ifdef COUNTER_I2C_ADDRESS
#define COUNTER_PARAM_GATE1_IN      2
#define COUNTER_PARAM_GATE1_OUT     3
#define COUNTER_PARAM_GATE2_IN      4
#define COUNTER_PARAM_GATE2_OUT     5
#define COUNTER_PARAM_GATE3_IN      6
#define COUNTER_PARAM_GATE3_OUT     7
#define COUNTER_PARAM_GATE4_IN      8
#define COUNTER_PARAM_GATE4_OUT     9
#define COUNTER_PARAM_GATE5_IN      10
#define COUNTER_PARAM_GATE5_OUT     11
#define COUNTER_PARAM_GATE6_IN      12
#define COUNTER_PARAM_GATE6_OUT     13
#define COUNTER_PARAM_GATE7_IN      14
#define COUNTER_PARAM_GATE7_OUT     15
#define COUNTER_PARAM_GATE8_IN      16
#define COUNTER_PARAM_GATE8_OUT     17

#define COUNTER_PARAM_GATE1_BG_IN      26
#define COUNTER_PARAM_GATE1_BG_OUT     27
#define COUNTER_PARAM_GATE2_BG_IN      28
#define COUNTER_PARAM_GATE2_BG_OUT     29
#define COUNTER_PARAM_GATE3_BG_IN      30
#define COUNTER_PARAM_GATE3_BG_OUT     31
#define COUNTER_PARAM_GATE4_BG_IN      32
#define COUNTER_PARAM_GATE4_BG_OUT     33
#define COUNTER_PARAM_GATE5_BG_IN      34
#define COUNTER_PARAM_GATE5_BG_OUT     35
#define COUNTER_PARAM_GATE6_BG_IN      36
#define COUNTER_PARAM_GATE6_BG_OUT     37
#define COUNTER_PARAM_GATE7_BG_IN      38
#define COUNTER_PARAM_GATE7_BG_OUT     39
#define COUNTER_PARAM_GATE8_BG_IN      40
#define COUNTER_PARAM_GATE8_BG_OUT     41
#endif

NIL_WORKING_AREA(waThreadCopySlave, 200);

NIL_THREAD(ThreadCopySlave, arg) {

  nilThdSleepMilliseconds(1000);

  while (true) {
#ifdef WEATHER_I2C_ADDRESS
    wireWakeup(WEATHER_I2C_ADDRESS);
#endif
#ifdef SCALE_I2C_ADDRESS
    wireWakeup(SCALE_I2C_ADDRESS);
#endif
    nilThdSleepMilliseconds(5000); // wait time to wake up
    copyFromSlave();
    nilThdSleepMilliseconds(60 * 1000);
  }
}

void copyFromSlave() {
#ifdef WEATHER_I2C_ADDRESS
  // wireCopyParameter(WEATHER_I2C_ADDRESS, WEATHER_TEMPERATURE_EXT, PARAM_EXT_TEMPERATURE);
  // wireCopyParameter(WEATHER_I2C_ADDRESS, WEATHER_TEMPERATURE_IN, PARAM_TEMPERATURE_IN);
  wireCopyParameter(WEATHER_I2C_ADDRESS, WEATHER_TEMPERATURE_BOARD, PARAM_EXT_TEMPERATURE);
  wireCopyParameter(WEATHER_I2C_ADDRESS, WEATHER_HUMIDITY, PARAM_EXT_HUMIDITY);
  // wireCopyParameter(WEATHER_I2C_ADDRESS, WEATHER_HUMIDITY_TEMP, PARAM_EXT_HUMIDITY_TEMP);
  wireCopyParameter(WEATHER_I2C_ADDRESS, WEATHER_LUMINOSITY, PARAM_LUMINOSITY);
  wireCopyParameter(WEATHER_I2C_ADDRESS, WEATHER_PRESSURE, PARAM_PRESSURE);
#endif

#ifdef SCALE_I2C_ADDRESS
  wireCopyParameter(SCALE_I2C_ADDRESS, SCALE_PARAM_WEIGHT, PARAM_WEIGHT);
  wireCopyParameter(SCALE_I2C_ADDRESS, SCALE_PARAM_WEIGHT_G, PARAM_WEIGHT_G);
  wireCopyParameter(SCALE_I2C_ADDRESS, SCALE_PARAM_WEIGHT_OFFSET, PARAM_WEIGHT_OFFSET);
  wireCopyParameter(SCALE_I2C_ADDRESS, SCALE_PARAM_WEIGHT_FACTOR, PARAM_WEIGHT_FACTOR);
#endif

#ifdef COUNTER_I2C_ADDRESS
  wireCopyParameter(COUNTER_I2C_ADDRESS, COUNTER_PARAM_GATE1_IN,  PARAM_GATE1_IN);
  wireCopyParameter(COUNTER_I2C_ADDRESS, COUNTER_PARAM_GATE1_OUT,  PARAM_GATE1_OUT);
  wireCopyParameter(COUNTER_I2C_ADDRESS, COUNTER_PARAM_GATE2_IN,  PARAM_GATE2_IN);
  wireCopyParameter(COUNTER_I2C_ADDRESS, COUNTER_PARAM_GATE2_OUT,  PARAM_GATE2_OUT);
  wireCopyParameter(COUNTER_I2C_ADDRESS, COUNTER_PARAM_GATE3_IN,  PARAM_GATE3_IN);
  wireCopyParameter(COUNTER_I2C_ADDRESS, COUNTER_PARAM_GATE3_OUT,  PARAM_GATE3_OUT);
  wireCopyParameter(COUNTER_I2C_ADDRESS, COUNTER_PARAM_GATE4_IN,  PARAM_GATE4_IN);
  wireCopyParameter(COUNTER_I2C_ADDRESS, COUNTER_PARAM_GATE4_OUT,  PARAM_GATE4_OUT);
  wireCopyParameter(COUNTER_I2C_ADDRESS, COUNTER_PARAM_GATE5_IN,  PARAM_GATE5_IN);
  wireCopyParameter(COUNTER_I2C_ADDRESS, COUNTER_PARAM_GATE5_OUT,  PARAM_GATE5_OUT);
  wireCopyParameter(COUNTER_I2C_ADDRESS, COUNTER_PARAM_GATE6_IN,  PARAM_GATE6_IN);
  wireCopyParameter(COUNTER_I2C_ADDRESS, COUNTER_PARAM_GATE6_OUT,  PARAM_GATE6_OUT);
  wireCopyParameter(COUNTER_I2C_ADDRESS, COUNTER_PARAM_GATE7_IN,  PARAM_GATE7_IN);
  wireCopyParameter(COUNTER_I2C_ADDRESS, COUNTER_PARAM_GATE7_OUT,  PARAM_GATE7_OUT);
  wireCopyParameter(COUNTER_I2C_ADDRESS, COUNTER_PARAM_GATE8_IN,  PARAM_GATE8_IN);
  wireCopyParameter(COUNTER_I2C_ADDRESS, COUNTER_PARAM_GATE8_OUT,  PARAM_GATE8_OUT);
#endif

}

#endif
